<!--
 * @Author: anshengtyp
 * @Date: 2023-12-13 10:54:22
 * @LastEditTime: 2024-01-29 17:19:40
 * @LastEditors: anshengtyp
 * @Descriptio： 延期任务申请统计
-->
<template>
  <div id="deferredTaskCount">
  </div>
</template>


<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons')
import resize from '../dashboard/mixins/resize';
import { getDelayTasks } from "@/api/indexCount.js";

export default {
  mixins: [resize],
  props: ['dept'],
  data() {
    return {
      chart: null,
      currentYear: new Date().getFullYear(),
      dataList: []
    };
  },

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
  },

  watch: {
    dept: {
      async handler(val) {
        const res = await getDelayTasks(this.currentYear);
        for (const key in res.data) {
          if (Object.hasOwn(res.data, key)) {
            const value = res.data[key];
            let target = val.find(item => item.deptId === key);
            if (target) {
              this.dataList.push({
                name: target.deptName,
                value: +value
              });
            }
          }
        }
        this.initChart();
      },
    }
  },

  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.chart.setOption({
        title: {
          text: `${this.currentYear}年度任务延期申请统计`
        },
        tooltip: {
          trigger: 'item',
          formatter: (params) => {
            return `${params.data.name}
           <br/><span style="display:inline-block;width:10px;height:10px;background-color:${params.color};border-radius:50%;"></span> ${this.currentYear}年度任务延期申请数量：
           ${params.percent === undefined ? 0 : params.percent}%`
          }
        },
        color: ['#82d7ad', '#658cee', '#687694', '#e7bb46', '#67C23A', '#F56C6C', '#409EFF'],
        series: [
          {
            type: 'pie',
            radius: '70%',
            avoidLabelOverlap: false,
            center: ['50%', '50%'],
            label: {
              show: true,
              position: 'outside',
              textStyle: {
                color: '#000', // 标签字体颜色
              },
              formatter: (params) => {
                let name = params.data.name;
                let value = params.data.value;
                return `{labelIcon|●}${name}：${value}个`
              },
              padding: [-20, -80, 0, -80],
              rich: {
                labelIcon: {
                  height: 14,
                  width: 14,
                  color: 'inherit'
                },
              }
            },
            labelLine: {
              length: 20,//第一段线长
              length2: 120 //第二段线长
            },
            data: this.dataList
          },
          {
            type: 'pie',
            radius: '70%',
            avoidLabelOverlap: false,
            center: ['50%', '50%'],
            label: {
              show: true,
              position: 'inside',
              textStyle: {
                color: '#fff', // 标签字体颜色
              },
              formatter: (value) => {
                return value.percent === undefined ? 0 + '%' : value.percent + '%';
              }
            },
            labelLine: {
              show: false
            },
            data: this.dataList
          }
        ]
      })
    }
  },
};
</script>


<style lang="scss" scoped>
#deferredTaskCount {
  width: 100%;
  height: 100%;
}
</style>
